package xyz.renyumeng.xkm.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.builder.GenericBuilder;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.renyumeng.xkm.dto.shop.ProductTypeDto;
import xyz.renyumeng.xkm.exception.XkmException;
import xyz.renyumeng.xkm.model.shop.ProductInfo;
import xyz.renyumeng.xkm.model.shop.ProductTypeInfo;
import xyz.renyumeng.xkm.result.Result;
import xyz.renyumeng.xkm.shop.mapper.ProductTypeInfoMapper;
import xyz.renyumeng.xkm.shop.service.ProductService;
import xyz.renyumeng.xkm.shop.service.ProductTypeInfoService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author renyumeng
 * @date Created in 14:50 2023/7/1
 */


@Service
@Transactional
public class ProductTypeInfoServiceImpl extends ServiceImpl<ProductTypeInfoMapper, ProductTypeInfo> implements ProductTypeInfoService {

    @Resource
    private ProductService productService;


    @Override
    public Result<List<ProductTypeInfo>> getChildrenType(Long parentId) {
        LambdaQueryWrapper<ProductTypeInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(ProductTypeInfo::getParentId, parentId);
        List<ProductTypeInfo> productTypeInfoList = baseMapper.selectList(qw);

        productTypeInfoList.forEach(productTypeInfo -> {
            Long productTypeInfoId = productTypeInfo.getId();
            boolean isChild = this.isChild(productTypeInfoId);

            productTypeInfo.setHasChildren(isChild);


        });
        return Result.ok(productTypeInfoList);
    }

    @Override
    public Result<Boolean> deleteTypeByIdWithProduct(Long typeId) {
        boolean isRemoved = this.removeById(typeId);
        if (BooleanUtil.isFalse(isRemoved)) {
            throw new XkmException(201, "删除失败!");
        }
        LambdaQueryWrapper<ProductInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductInfo::getTypeId, typeId);
        boolean isRemoved4Product = productService.remove(queryWrapper);
        return Result.ok(isRemoved).message("删除成功!");
    }

    @Override
    public Result<Boolean> updateTypeByIdWithProduct(ProductTypeDto productTypeDto) {
        ProductTypeInfo productTypeInfo = BeanUtil.copyProperties(productTypeDto, ProductTypeInfo.class);
        boolean isUpdated = this.updateById(productTypeInfo);
        if (BooleanUtil.isFalse(isUpdated)) {
            throw new XkmException(201, "修改失败!");
        }
        Long typeId = productTypeInfo.getId();
        LambdaQueryWrapper<ProductInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductInfo::getTypeId, typeId);
        ProductInfo productInfo = GenericBuilder
                .of(ProductInfo::new)
                .with(ProductInfo::setTypeId, typeId)
                .build();
        boolean isUpdatedProduct = productService.update(productInfo, queryWrapper);
        return Result.ok(isUpdated).message("修改成功!");
    }

    @Override
    public Result<Boolean> addType(ProductTypeDto productTypeDto) {
        ProductTypeInfo productTypeInfo = BeanUtil.copyProperties(productTypeDto, ProductTypeInfo.class);
        boolean isSaved = this.save(productTypeInfo);
        if (BooleanUtil.isFalse(isSaved)) {
            throw new XkmException(201, "新增失败!");
        }
        return Result.ok(isSaved).message("添加类型成功!");
    }

    private boolean isChild(Long productTypeInfoId) {
        LambdaQueryWrapper<ProductTypeInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(ProductTypeInfo::getParentId, productTypeInfoId);
        Integer count = baseMapper.selectCount(qw);
        return count > 0;
    }
}
